<template>
  <el-container class="homeContainer">
    <!-- 头部区域开始 -->
    <el-header>
      <!-- <img src="../assets/logo.png" alt="" /> -->
      <div style="color:#fff; text-align center; margin-left:80px">商品管理系统</div>
      <!-- <el-button type="info" @click="logout">退出</el-button> -->

      <el-dropdown>
        <el-avatar class="user_avatar"> Admin </el-avatar>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="logout">
            <i class="el-icon-right"></i>
            退出</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!-- 头部区域结束 -->
    <el-container>
      <!-- 左侧菜单栏开始 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 菜单笔记
         default-active 默认激活哪一个
          -->
        <el-menu
          default-active="activePath"
          background-color="#373d3f"
          text-color="#fff"
          active-text-color="#097cb8"
          unique-opened
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 菜单名称 -->
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 菜单名称 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 左侧菜单栏结束 -->
      <!-- 右侧主体开始 -->
      <el-main>
        <!-- 显示路由 -->
        <router-view></router-view>
      </el-main>
      <!-- 右侧主体结束 -->
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      //菜单
      menuList: [
        {
          id: 103,
          authName: "商品管理",
          path: "null",
          children: [
            {
              id: 3001,
              authName: "商品列表",
              path: "categories",
              children: []
            }
          ]
        },
      ],
      //控制是否展开菜单
      isCollapse: false,
      // 一级菜单的icon图标
      iconsObj: {
        101: "el-icon-monitor",
        102: "el-icon-edit",
        103: "el-icon-folder",
        104: "el-icon-chat-dot-round",
        105: "el-icon-setting"
      },
      //修改个人信息窗口
      editUserInfoPage: false,
      //修改密码窗口
      editUserPwdPage: false,
      //修改密码表单
      editUserPwdForm: {
        oldPassword: "",
        newPassword: ""
      }
    };
  },

  methods: {
    //   退出
    logout() {
      console.log("退出");
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //请求左侧菜单
    async getMenuList() {
      console.log("开始获取菜单==》");
      //   const { data: res } = await this.$http.get("menus");
      //   // console.log(res);
      //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      //   this.$message.success(res.meta.msg);
      //   this.menuList = res.data;
      //   console.log(res, "menus");
      //   console.log(JSON.stringify(this.menuList));
    },
    //控制是否展开菜单
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    errorHandler() {
      return true;
    },
    //查看自己的网站
    go2MySite() {
      this.$router.push("/site_welcome");
    },
    //编辑个人信息
    editUserInfo() {
      this.editUserInfoPage = true;
    },
    //修改密码窗口
    editUserPwd() {
      this.editUserPwdPage = true;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    //操作菜单
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }
  },
  created() {
    this.getMenuList();

    //刷新后停留在该页面
    this.activePath = window.sessionStorage.getItem("activePath");
  }
};
</script>
<style scoped>
.homeContainer {
  height: 100%;
}
.el-header {
  background-color: #b3c0d1;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #e9eef3;
  background-color: #373d3f;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #373d3f;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  /* text-align: center; */
  /* line-height: 160px; */
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
/* 控制菜单展开收缩的按钮 */
.toggle-button {
  background: #373d3f;
  font-size: 10px;
  text-align: center;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.user_avatar {
  margin-top: 20px;
  cursor: pointer;
}
.el-menu {
  border-right: 0;
}
</style>
